package com.softhinker.ke24.unit3.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.net.URL;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

import com.softhinker.ke24.unit3.engine.QuestionProvider;
import com.softhinker.ke24.unit3.listener.QuestionListener;
import com.softhinker.ke24.unit3.listener.UserProfileListener;
import com.softhinker.ke24.unit3.model.Affordability;
import com.softhinker.ke24.unit3.model.GreedLevel;
import com.softhinker.ke24.unit3.model.ProfessionLevel;
import com.softhinker.ke24.unit3.model.UserProfile;

public class QuestionPanel extends JPanel implements QuestionListener, UserProfileListener {

	private static final long serialVersionUID = 1115002365521695110L;

	final static int GAP = 10;
	private JButton backButton;
	private JButton nextButton;
	private JButton submitButton;
	private QuestionProvider questionProvider;
	private JLabel questionLabel;
	private JLabel errorLabel;
	private JLabel titleLabel;
	private JLabel imageLabel;
	private JPanel controlPanel = new JPanel();
	private JPanel contentPanel = new JPanel();

	public QuestionPanel(QuestionProvider questionProvider) {
		setLayout(new BorderLayout());

		this.questionProvider = questionProvider;
		this.questionProvider.setQuestionListener(this);
		this.questionProvider.addResultListener(this);
		
		backButton = new JButton();
		nextButton = new JButton();
		submitButton = new JButton();
		questionLabel = new JLabel();
		errorLabel = new JLabel();
		errorLabel.setForeground(Color.red);
		questionLabel.setText(this.questionProvider.getFirstQuestion());
		imageLabel = new JLabel();

		titleLabel = new JLabel("Questions");
		add(titleLabel, BorderLayout.NORTH);
		add(createQuestion(), BorderLayout.CENTER);
		add(createControls(), BorderLayout.SOUTH);
	}

	private JComponent createQuestion() {
		this.contentPanel.removeAll();
		this.contentPanel.repaint();
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));

		contentPanel.add(this.questionLabel);
		contentPanel.add(questionProvider.getFirstOption());
		contentPanel.add(this.errorLabel);
		
        this.imageLabel.setIcon(new ImageIcon(this.getClass().getResource("1.gif")));
        contentPanel.add(this.imageLabel);
		
		return contentPanel;
	}

	private JComponent createControls() {
		controlPanel.removeAll();
		controlPanel.repaint();
		
		backButton.setText("Back");
		nextButton.setText("Next");
		submitButton.setText("Submit");
		
		backButton.setEnabled(false);
		nextButton.setEnabled(true);
		submitButton.setEnabled(false);

		backButton.addActionListener(questionProvider);
		nextButton.addActionListener(questionProvider);
		submitButton.addActionListener(questionProvider);
		nextButton.setActionCommand("1");
		submitButton.setActionCommand("Submit");

		controlPanel.add(backButton, 0);
		controlPanel.add(nextButton, 1);
		controlPanel.add(submitButton, 2);
		return controlPanel;
	}

	@Override
	public void renderQuestion(String question) {
		this.questionLabel.setText(question);
	}

	@Override
	public void updateActionCommand(String backCommand, String nextCommand) {
		this.backButton.setActionCommand(backCommand);
		this.nextButton.setActionCommand(nextCommand);

		this.backButton.setEnabled(backCommand != null);
		this.nextButton.setEnabled(nextCommand != null);

		if(backCommand == null){
			this.imageLabel.setIcon(new ImageIcon(this.getClass().getResource("1.gif")));
		}
		else{
			int i = Integer.parseInt(backCommand);
			String iconName = String.valueOf(i+2);
			System.out.println(iconName + ".jpg");
			this.imageLabel.setIcon(new ImageIcon(this.getClass().getResource(iconName + ".jpg")));
		}
		
		if (nextCommand == null) {
			submitButton.setEnabled(true);
		} else{
			submitButton.setEnabled(false);
		}
	}

	@Override
	public void displayError(String error) {
		errorLabel.setText(error);
	}

	@Override
	public void publishUserProfile(UserProfile userProfile) {
		// Clean up the UI
		this.contentPanel.removeAll();
		this.contentPanel.repaint();

		this.backButton.setText("Restart");
		this.backButton.setActionCommand("Restart");
		submitButton.setEnabled(false);
		
		this.titleLabel.setText("[Your answers]");
		
		// Display result
		this.contentPanel.setLayout(new SpringLayout());

		GreedLevel greedLevel = userProfile.getGreedLevel();
		Affordability affordability = userProfile.getAffordability(); 
		ProfessionLevel professionLevel = userProfile.getProfessionLevel();
		
		this.contentPanel.add(new JLabel(" "));
		this.contentPanel.add(new JLabel(" "));
		
		this.contentPanel.add(new JLabel("Expected ROI : "));
		this.contentPanel.add(new JLabel(String.valueOf(greedLevel
				.getExpectedROI() * 100) + "%"));

		this.contentPanel.add(new JLabel("Investment time span : "));
		this.contentPanel.add(new JLabel(String.valueOf(greedLevel
				.getInvestmentTimeSpan()) + " days"));

		this.contentPanel.add(new JLabel("Accepted selling price : "));
		String comparator = null;
		switch (greedLevel.getAcceptedSellingPoint()) {
		case 0: {
			comparator = "could be between buying price and expected ROI";
			break;
		}
		case 1: {
			comparator = "must equal to expected ROI";
			break;
		}
		case -1: {
			comparator = "could be lower than buying price";
			break;
		}
		}
		this.contentPanel.add(new JLabel(comparator));

		this.contentPanel.add(new JLabel("Affordable maximum lost : "));
		this.contentPanel.add(new JLabel(String.valueOf(affordability
				.getAffordableMaximumLost() * 100) + "%"));

		this.contentPanel.add(new JLabel("Continue to invest ? "));
		String answer = null;
		switch (affordability.getContinueToInvest()) {
		case 0: {
			answer = "Maybe";
			break;
		}
		case 1: {
			answer = "Yes";
			break;
		}
		case -1: {
			answer = "No";
			break;
		}
		}
		this.contentPanel.add(new JLabel(answer));

		this.contentPanel.add(new JLabel("Years of investment experience : "));
		this.contentPanel.add(new JLabel(String.valueOf(professionLevel
				.getYearsOfInvestment()) + " years"));

		this.contentPanel.add(new JLabel("Working in financial industry ? "));
		this.contentPanel.add(new JLabel(String.valueOf(professionLevel
				.isInFinanceIndustry())));

		SpringUtilities.makeCompactGrid(this.contentPanel, 8, 2, 1, 1, 1, 1);
	}

	@Override
	public void restart() {
		this.titleLabel.setText("Questions");
		questionLabel.setText(questionProvider.getFirstQuestion());
		createQuestion();
		createControls();
	}
}
